﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Model;

namespace Common.Util
{
    public class Util
    {
        //计算两个集合的相似度，相似度计算方式，交集大小/并集大小
        public static float CalcSimilarity(List<string> left, List<string> right)
        {
            HashSet<String> mergeSet = new HashSet<string>(); //并集
            HashSet<String> interSet = new HashSet<string>(); //交集

            foreach (String stockCode in left)
            {
                if (!mergeSet.Contains(stockCode))
                {
                    mergeSet.Add(stockCode);
                }
                if (right.Contains(stockCode))
                {
                    interSet.Add(stockCode);
                }
            }

            foreach (String stockCode in right)
            {
                if (!mergeSet.Contains(stockCode))
                {
                    mergeSet.Add(stockCode);
                }
            }

            return (1.0f * interSet.Count) / mergeSet.Count;
        }

        public static float CalcSimilarity(HashSet<string> left, HashSet<string> right)
        {
            return CalcSimilarity(left.ToList(), right.ToList());
        }

        public static float CalcSimilarity(HashSet<Stock> left, HashSet<Stock> right)
        {
            var leftSet = new HashSet<string>();
            foreach (var stock in left)
            {
                leftSet.Add(stock.StockCode);
            }

            var rightSet = new HashSet<string>();
            foreach (var stock in right)
            {
                rightSet.Add(stock.StockCode);
            }
            return CalcSimilarity(leftSet, rightSet);
        }

        public static string GetJoinKey(string leftStockCode, string rightStockCode)
        {
            var leftCode = leftStockCode.GetHashCode();
            var rightCode = rightStockCode.GetHashCode();
            return leftCode > rightCode ? leftStockCode + ":" + rightStockCode : rightStockCode + ":" + leftStockCode;
        }

        public static string[] SplitWords(string words)
        {
            char[] sp1 = {','};
            string[] terms = null;
            terms = words.Split(sp1, StringSplitOptions.RemoveEmptyEntries);
            return terms;
        }

        public static string MergeWords(Dictionary<string, float> dictionary)
        {
            var stringBuilder = new StringBuilder();
            foreach (var pair in dictionary)
            {
                if (pair.Value > 0.8)
                {
                    stringBuilder.Append(pair.Key);
                    stringBuilder.Append(",");
                }
            }
            if (stringBuilder.Length > 0)
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }
            return stringBuilder.ToString();
        }

        public static string MergeWords(List<string> list)
        {
            var stringBuilder = new StringBuilder();
            foreach (var s in list)
            {
                stringBuilder.Append(s);
                stringBuilder.Append(",");
            }
            if (stringBuilder.Length > 0)
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }
            return stringBuilder.ToString();
        }

        public static int StrCount(string input, string sub)
        {
            if (sub.Length == 0)
            {
                return 0;
            }
            return (input.Length - input.Replace(sub, "").Length) / sub.Length;
        }
        private static readonly DateTime StartTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
        public static long GetTimeStamp(DateTime dateTime)
        {
            return (long)(dateTime - StartTime).TotalMilliseconds; // 相差秒数
        }
    }
}